package com.opentd.graphics.screen;

import java.awt.Graphics;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import com.opentd.game.Game;
import com.opentd.graphics.Level;
import com.opentd.gui.Frame;

public class LevelSelectScreen extends Screen{

	private static LevelSelectWindow selector;
	
	private static LevelSelectScreen instance = new LevelSelectScreen();
	
	public class LevelSelectWindow extends javax.swing.JFrame {

	    /**
	     * Creates new form LevelSelectWindow
	     */
	    public LevelSelectWindow() {
	        initComponents();
	        updateLevels();
	        addWindowListener(new WindowListener(){
				@Override
				public void windowActivated(WindowEvent arg0) {}

				@Override
				public void windowClosed(WindowEvent arg0) {
					Game.State.currentScreen = Game.State.MAIN_SCREEN;
				}

				@Override
				public void windowClosing(WindowEvent arg0) {
					Game.State.currentScreen = Game.State.MAIN_SCREEN;
				}

				@Override
				public void windowDeactivated(WindowEvent arg0) {}

				@Override
				public void windowDeiconified(WindowEvent arg0) {}

				@Override
				public void windowIconified(WindowEvent arg0) {}

				@Override
				public void windowOpened(WindowEvent arg0) {}
	        	
	        });
	    }
	    
	    private void updateLevels(){
	    	DefaultListModel model = new DefaultListModel(){
	    		
	    	};
	    	for(Level level : Game.levels){
	    		model.addElement(level.title + "(" + level.waves.size() + " Waves)");
	    	}
	    	levelList.setModel(model);
	    }

	    /**
	     * This method is called from within the constructor to initialize the form.
	     * WARNING: Do NOT modify this code. The content of this method is always
	     * regenerated by the Form Editor.
	     */
	    @SuppressWarnings("unchecked")
	    // <editor-fold defaultstate="collapsed" desc="Generated Code">
	    private void initComponents() {

	        jScrollPane1 = new javax.swing.JScrollPane();
	        levelList = new javax.swing.JList();
	        playButton = new javax.swing.JButton();
	        cancelButton = new javax.swing.JButton();

	        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
	        setResizable(false);
	        setTitle("Select Level - Open TD");

	        jScrollPane1.setViewportView(levelList);

	        playButton.setText("Play");
	        playButton.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                playButtonActionPerformed(evt);
	            }
	        });

	        cancelButton.setText("Cancel");
	        cancelButton.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                cancelButtonActionPerformed(evt);
	            }
	        });

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
	                    .addGroup(layout.createSequentialGroup()
	                        .addComponent(playButton)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
	                        .addComponent(cancelButton)))
	                .addContainerGap())
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(playButton)
	                    .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	                .addContainerGap())
	        );

	        pack();
	        setLocation(Frame.instance.getLocation().x + ((Frame.instance.getWidth() / 2) - (this.getWidth() / 2)), Frame.instance.getLocation().y + ((Frame.instance.getHeight() / 2) - (this.getHeight() / 2)));
	        updateLevels();
	    }// </editor-fold>

	    private void playButtonActionPerformed(java.awt.event.ActionEvent evt) {
	    	int index = levelList.getSelectedIndex();
	    	if(index < 0){
	    		JOptionPane.showMessageDialog(this, "No level selected", "Error:", JOptionPane.ERROR_MESSAGE);
	    		return;
	    	}
	    	Game.selectedLevelIndex = index;
	    	Level l = Game.levels.elementAt(index);
	        Game.selectedLevel = new Level(l.title, l.health, l.money, l.waves, l.map);
	        if(Game.State.goToGameAfterSelectLevel){
	        	Game.State.currentScreen = Game.State.GAME_SCREEN;
	        	Game.State.goToGameAfterSelectLevel = false;
	        }else{
	        	Game.State.currentScreen = Game.State.MAIN_SCREEN;
	        }
	        setVisible(false);
	    }

	    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
			Game.State.currentScreen = Game.State.MAIN_SCREEN;
	        setVisible(false);
	    }
	    
	    // Variables declaration - do not modify
	    private javax.swing.JButton cancelButton;
	    private javax.swing.JScrollPane jScrollPane1;
	    private javax.swing.JList levelList;
	    private javax.swing.JButton playButton;
	    // End of variables declaration
	}
	
	public static void draw(Graphics g) {
		showLevelSelectWindow();
		g.fillRect(0, 0, Game.panel.getWidth(), Game.panel.getHeight());
	}

	private static void showLevelSelectWindow() {
		if(selector == null){
			selector = instance.new LevelSelectWindow();
		}
		if(!selector.isVisible()){
			selector.updateLevels();
			selector.setVisible(true);
		}
	}

}
